#include <QFile>
#include <QWebSettings>
#include <QDebug>
#include <QDesktopWidget>
#include <QApplication>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QFileInfo>
#include <QFileDialog>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>

#include "mainwindow.h"
#include "graphicshybridview.h"

MainWindow::MainWindow(QString debug, QWidget *parent)
	: QMainWindow(parent),
	m_hybridView(0),
	m_panningEnabled(false)
{
    QWidget *layoutWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout();
    layoutWidget->setLayout(layout);

	QLabel *message = new QLabel("No valid widget given as argument. Choose a widget's main HTML file.\n\nPassed arguments: " + debug);
    layout->addWidget(message);

    QPushButton *fileButton = new QPushButton(tr("Choose a file"));
    connect(fileButton, SIGNAL(clicked()), this, SLOT(selectFile()));
    layout->addWidget(fileButton);

    setWindowTitle("Widget Launcher");
    setCentralWidget(layoutWidget);
}

MainWindow::MainWindow(QString widgetIdentifier, QString filePath, bool panningEnabled, QWidget *parent)
	: QMainWindow(parent),
	m_hybridView(0),
	m_view(this),
	m_panningEnabled(panningEnabled)
{
    openWidget(widgetIdentifier, filePath);
}

void MainWindow::selectFile()
{
    QFileInfo f(QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::currentPath(), tr("HTML files (*.html *.htm)")));
    if (QFile::exists(f.canonicalFilePath())) {
        openWidget("widget-identifier", f.canonicalFilePath());
    }
}

void MainWindow::openWidget(QString widgetIdentifier, QString filePath)
{
    m_hybridView = new GraphicsHybridView(this, 0, widgetIdentifier);

	// If panning is enabled, make sure the web view is resized when so required.
	if (m_panningEnabled)
	{
		connect(m_hybridView, SIGNAL(contentsSizeChanged(QSize)),
				this, SLOT(contentsSizeChanged(QSize)));
	}
    m_hybridView->load(QUrl::fromLocalFile(filePath));

    QMenu *resolutionMenu = menuBar()->addMenu(tr("Resolution"));
    QAction *w240h320 = new QAction(tr("240 x 320 (E75)"), this);
    connect(w240h320, SIGNAL(triggered()), this, SLOT(resizeW240H320()));
    resolutionMenu->addAction(w240h320);

    QAction *w320x240 = new QAction(tr("320 x 240 (E71)"), this);
    connect(w320x240, SIGNAL(triggered()), this, SLOT(resizeW320H240()));
    resolutionMenu->addAction(w320x240);

    QAction *w360h640 = new QAction(tr("360 x 640 (N97 portrait)"), this);
    connect(w360h640, SIGNAL(triggered()), this, SLOT(resizeW360H640()));
    resolutionMenu->addAction(w360h640);

    QAction *w640h360 = new QAction(tr("640 x 360 (N97 landscape)"), this);
    connect(w640h360, SIGNAL(triggered()), this, SLOT(resizeW640H360()));
    resolutionMenu->addAction(w640h360);

    QAction *w800h480 = new QAction(tr("800 x 480 (N900)"), this);
    connect(w800h480, SIGNAL(triggered()), this, SLOT(resizeW800H480()));
    resolutionMenu->addAction(w800h480);

	if (m_panningEnabled)
	{
		m_view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
		m_view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		m_view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
		m_view.setFrameStyle(QFrame::NoFrame);

		m_pannableView = new PannableView(Qt::Vertical | Qt::Horizontal);
		m_pannableView->setWidget(m_hybridView);

		m_scene.addItem(m_pannableView);
		m_view.setScene(&m_scene);
	}
	else
	{
		m_scene.addItem(m_hybridView);
	}

	m_view.setScene(&m_scene);
	m_view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	m_view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setCentralWidget(&m_view);

	resizeView(360, 640);
	setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
}

void MainWindow::resizeView(int width, int height)
{
	if (m_panningEnabled)
	{
		m_pannableView->setMinimumSize(width, height);
		m_pannableView->setPreferredSize(width, height);
		m_pannableView->setMaximumSize(width, height);
		m_view.setFixedSize(width + 4, height + 4);
		setFixedSize(width + 4, height + menuBar()->size().height() + 4);
		m_hybridView->setMinimumSize(0, 0);
	}
	else
	{
		m_hybridView->resize(width, height);
		m_view.setFixedSize(width + 4, height + 4);
		setFixedSize(width + 4, height + menuBar()->size().height() + 4);
	}
}

void MainWindow::contentsSizeChanged(QSize newSize)
{
	m_hybridView->setMinimumSize(newSize.width(), newSize.height());
}

void MainWindow::resizeW240H320()
{
    resizeView(240, 320);
}

void MainWindow::resizeW320H240()
{
    resizeView(320, 240);
}

void MainWindow::resizeW360H640()
{
    resizeView(360, 640);
}

void MainWindow::resizeW640H360()
{
    resizeView(640, 360);
}

void MainWindow::resizeW800H480()
{
    resizeView(800, 480);
}
